//
//  HeartFiveView.m
//  B2C
//
//  Created by xiaowu on 2017/7/31.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "HeartFiveView.h"

static NSInteger baseImageTag = 35234;
static CGFloat commonOffset = 5;

#define NormalHeartSize  CGSizeMake(15, 15)
#define LittleHeartSize  CGSizeMake(10, 10)

@interface HeartFiveView ()

@property (nonatomic, assign) CGFloat point;

@end

@implementation HeartFiveView

+ (CGSize)getFiveHeartViewSizeWithPoint:(CGFloat)point withType:(HeartType)type {
    point = 5;
    if (type == HeartTypeForNormal) {
        return CGSizeMake(point * (NormalHeartSize.width + commonOffset) - commonOffset, NormalHeartSize.height);
    } else {
        return CGSizeMake(point * (LittleHeartSize.width + commonOffset) - commonOffset, LittleHeartSize.height);
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _point = 0.0;
        _canEdit = NO;
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _point = 0.0;
        _canEdit = NO;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
}

- (void)setImageViewWithPoint:(CGFloat)point withType:(HeartType)type {
    self.point = point;
    self.selectCount = point;
    if (point > 5) {
        point = 5;
    }
    
    for (NSInteger i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button.imageView setContentMode:UIViewContentModeCenter];
        [button setImage:[UIImage imageNamed:@"star_ash"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"star"] forState:UIControlStateSelected];
        
        if ((point > i + 0.5)&& (point < i + 1)) {
            [button setImage:[UIImage imageNamed:@"star"] forState:UIControlStateSelected];//star_half
        }
        
        if (type == HeartTypeForNormal) {
            button.frame = CGRectMake(i * (NormalHeartSize.width + commonOffset), 0, NormalHeartSize.width, NormalHeartSize.height);
        } else {
            button.frame = CGRectMake(i * (LittleHeartSize.width + commonOffset), 0, LittleHeartSize.width, LittleHeartSize.height);
        }
        button.tag = baseImageTag + i;
        if (i < point) {
            [button setSelected:YES];
        }
        if (self.canEdit) {
            [button addTarget:self action:@selector(heartButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        button.userInteractionEnabled = self.canEdit;
        [self addSubview:button];
    }
}

- (void)heartButtonAction:(id)sender {
    UIButton *selectButton = (UIButton *)sender;
    for (int i = 0; i < 5; i++) {
        UIButton *temp = (UIButton *)[self viewWithTag:baseImageTag+i];
        if (temp.tag > selectButton.tag) {
            [temp setSelected:NO];
        } else {
            [temp setSelected:YES];
        }
    }
    self.selectCount = selectButton.tag-baseImageTag;
    
    if (self.block) {
        self.block(selectButton.tag-baseImageTag);
    }
}

@end
